global with sharing class ReturnInStoreVisitIDWS {
    WebService static String GetInStoreVisitId(String outletId,String userId,DateTime StartDate,DateTime EndDate)
    { 
        //处理routing plan and visit
        
        List<Routing_Plan_And_Visit__c> rpavList=[select Id,Visit_Date__c from Routing_Plan_And_Visit__c 
                                                  where Visit_Date__c =:StartDate.Date() and OwnerId=:userId];
        //如果不存在就创建新的routing plan and visit                        
        if(rpavList==NULL ||  rpavList.size()==0)
        {
            Routing_Plan_And_Visit__c rC=new Routing_Plan_And_Visit__c(Visit_Date__c=StartDate.Date(),OwnerId=userId);
            rpavList=new List<Routing_Plan_And_Visit__c>();
            //新增routing plan and visit
            insert rC;  
            rpavList.add(rC);
        }
          
        //处理Routing Plan
        List<RoutingPlan__c> rpList=new List<RoutingPlan__c>([select Id,OutletName__c,StartDateTime__c,Plan_Visit_Date__c
                                                                from RoutingPlan__c where 
                                                                OutletName__c =:outletId and Plan_Visit_Date__c =:StartDate.Date()]);
      
        //更新状态
        for(RoutingPlan__c rp:rpList)
        {
            rp.PlanStatus__c='done';
            update rp;
        }
        
        //处理in store visit
        List<In_Store_Visit__c>  VList=[select Id,HaveSurvey__c,StartDate__c,OutletName_del__c,Start_Time__c,End_Time__c from In_Store_Visit__c 
                                       where StartDate__c =:StartDate.Date() and OutletName_del__c =:outletId 
                                       and RoutingPlanAndVisit__r.OwnerId=:userId];
                                       
       if(VList==NULL || VList.size()==0)
       {
            Id RoutingPlanId;
            //Datetime dt=DateTime.newInstance(visitDate.year(), visitDate.month(),visitDate.day(),12,10,00);
            if(rpList==NULL || rpList.size()==0)
                RoutingPlanId=NULL;
            else
                RoutingPlanId=rpList[0].Id;
                
            In_Store_Visit__c is=new In_Store_Visit__c(Start_Time__c=StartDate,
                               End_Time__c=EndDate,
                               RoutingPlanAndVisit__c=rpavList[0].Id,
                               OutletName_del__c=outletId,
                               RoutingPlanItem__c=RoutingPlanId,
                               HaveSurvey__c = true);
           insert is;
           VList.add(is);
       }
       else
       {
           VList[0].HaveSurvey__c = true;
           update VList[0];
       }
       return VList[0].Id;
    }
    
    static testmethod void test() 
    {
        //Date.newInstance(2010,12,7);
        Account acc=new Account();
        acc.Name='test';
        acc.outletCode__c='test';
         acc.Channel__c='百货店';            
         acc.Contact__c='aaa';            
         acc.No_of_checkout__c=22;            
         acc.ContactPhoneNumber__c='1234567456';
         
        insert acc;
       // String idStr=ReturnInStoreVisitIDWS.GetInStoreVisitId(acc.Id,UserInfo.getUserId(),Date.newInstance(2010,12,20));
      // ReturnInStoreVisitIDWS.GetInStoreVisitId('001O0000003G2RL','00590000000Nhjp',Date.newInstance(2010,12,21));
       
       // System.Debug('*************************'+idStr);
    }
}